package com.jdew.thkj.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.adapter.MyFansAdapter;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.FansBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.manager.WrapContentLinearLayoutManager;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.WeiboDialogUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/4/20 0020.
 */

public class FansActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.noresult)
    TextView noresult;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerView;
    @Bind(R.id.smartRefresh)
    SmartRefreshLayout smartRefreshLayout;
    @Bind(R.id.result_view)
    RelativeLayout resultView;
    private List<FansBean.DataBean> list = new ArrayList<>();
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    private MyFansAdapter adapter;
    private int page = 1;
    private int type;

    @Override
    public int bindLayout() {
        return R.layout.activity_fans;
    }

    @Override
    public void initView(View view) {
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        adapter = new MyFansAdapter(this, list);
        setRcycleview(recyclerView, adapter);
        type = getIntent().getIntExtra("type", 3);
        if (type == 3) {
            title.setText("我的粉丝");
        } else if (type == 0) {
            title.setText("直属教练");
        } else if (type == 1) {
            title.setText("直属惠粉");
        } else if (type == 2) {
            title.setText("直属高级惠员");
        } else if (type == 4) {
            title.setText("直属子公司");
        }
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        smartRefreshLayout.setRefreshHeader(new MaterialHeader(this));
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                GetUserList();
                smartRefreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                smartRefreshLayout.setEnableLoadMore(true);
                GetUserList();
                smartRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            default:
                break;
        }
    }

    public void GetUserList() {
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(this);
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetUserList(sp.getString("userPhone", ""), type + "", page + "", RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "UserPhone" + sp.getString("userPhone", "") + "SpareTwo" + type + "PageIndex" + page)).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<FansBean>() {
            @Override
            public void onCompleted() {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onError(Throwable e) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
            }

            @Override
            public void onNext(FansBean bean) {
                WeiboDialogUtils.closeDialog(mWeiboDialog);
                if (bean.getCode().equals("0")) {
                    if (page == 1) {
                        list.clear();
                    }
                    list.addAll(bean.getData());
                    if (bean.getData().size() < 20 && bean.getData().size() > 0) {
                        smartRefreshLayout.setEnableLoadMore(false);
                    } else {
                        if (list.size() == 0 && page == 1) {
                            smartRefreshLayout.setVisibility(View.GONE);
                            resultView.setVisibility(View.VISIBLE);
                            noresult.setText("您还没有粉丝哦");
                            return;
                        }
                    }
                    adapter.setDataList(list);
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                } else if (bean.getCode().equals("-1")) {
                    if (list.size() == 0 && page == 1) {
                        noresult.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        page = 1;
        GetUserList();
    }
}
